package parser

import (
	"fmt"
	"log"
	"luiz/crawler/engine"
	"luiz/crawler/model"
	"regexp"
	"strconv"
)

//todo:在profile.go中摘抄属性函数
//todo:年龄
const userRe = `class="m-btn purple">[^<]+</div>`

func ParseProfile(contents []byte) engine.ParseResult {
	compile := regexp.MustCompile(userRe)
	findString := compile.FindAllString(string(contents), -1)
	//如果正则匹配失败，记录日志找一下失败的点
	if findString == nil {
		log.Printf("contents no found %v", (string(contents)))
	}
	result := engine.ParseResult{}
	if len(findString) != 7 {
		//todo:在run中间中加入一个终止条件，空数组就continue,todo 找一个分级别的日志管理系统
		//todo:字符检查
		log.Printf("找到的记录不足7个，长度为%v，内容为%v", len(findString), findString)
		return result
	}
	profile := model.Profile{}
	for index, match := range findString {
		//婚姻情况，年龄，身高，工作地，星座，月收入，学历
		switch index {
		case 1:
			profile.Marriedsit = match
			fmt.Println("婚姻情况:" + match)
		case 2:
			atoi, err := strconv.Atoi(match)
			if err != nil {
				profile.Age = uint8(atoi)
				fmt.Println("年龄:" + match)
			} else {
				log.Printf("转换失败，元素为%v,记录为%v", match, string(contents))
			}
		case 3:
			profile.Height = match
			fmt.Println("身高是：" + match)
		case 4:
			profile.WorkPlace = match
			fmt.Println("工作递点:" + match)
		case 5:
			profile.Constellation = match
			fmt.Println("星座:" + match)
		case 6:
			incomes, err := strconv.ParseFloat(match, 32)
			if err != nil {
				log.Printf("转换失败，元素为%v,记录为%v", match, string(contents))
				continue
			}
			fmt.Println("工资:" + match)
			profile.Income = float32(incomes)
		case 7:
			profile.Education = match
			fmt.Println("教育情况:" + match)
		}
		println("_________________________________")
		profiles := []interface{}{profile}
		result.Items = profiles
	}
	return result

}
